package so.shanku.essential.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import so.shanku.essential.R;

/**
 * the view that wrapps a divider header and a normal list item. The listview sees this as 1 item
 *
 * @author Emil Sjölander
 */
public class WrapperView extends ViewGroup {

	View mItem;

	View mHeader;
	private int item_margin_top_bottom=0;
	/**
	 * head和item的相对位置
	 */
	private RelativeType default_type=RelativeType.CENTER;
	public enum RelativeType{
		CENTER,TOP
	}

	public RelativeType getDefault_type() {
		return default_type;
	}

	public void setDefault_type(RelativeType default_type) {
		this.default_type = default_type;
	}

	public WrapperView(Context c) {
		super(c);
		item_margin_top_bottom = getResources().getDimensionPixelSize(R.dimen.common_margin);
	}

	public WrapperView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public boolean hasHeader() {
		return mHeader != null;
	}

	public View getItem() {
		return mItem;
	}

	public View getHeader() {
		return mHeader;
	}

	/**
	 * 组合item和headerview到控件里
	 *
	 * @param item
	 * @param header
	 * @param divider
	 * @param dividerHeight
	 */
	public void update(View item, View header, Drawable divider, int dividerHeight) {

		//every wrapperview must have a list item
		if (item == null) {
			throw new NullPointerException("List view item must not be null.");
		}

		//only remove the current item if it is not the same as the new item. this can happen if wrapping a recycled view
		if (this.mItem != item) {
			removeView(this.mItem);
			this.mItem = item;
			final ViewParent parent = item.getParent();
			if (parent != null && parent != this) {
				if (parent instanceof ViewGroup) {
					((ViewGroup) parent).removeView(item);
				}
			}
			addView(item);
		}

		//same logik as above but for the header
		if (this.mHeader != header) {
			if (this.mHeader != null) {
				removeView(this.mHeader);
			}
			this.mHeader = header;
			if (header != null) {
				addView(header);
			}
		}

		invalidate();
	}

	/**
	 * 计算整个条目的高度  head/2+item
	 *
	 * @param widthMeasureSpec
	 * @param heightMeasureSpec
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
		int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth,
				MeasureSpec.EXACTLY);
		int measuredHeight = 0;

		//measure header or divider. when there is a header visible it acts as the divider
		if (mHeader != null) {
			LayoutParams params = mHeader.getLayoutParams();
			if (params != null && params.height > 0) {
				mHeader.measure(childWidthMeasureSpec,
						MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY));
			} else {
				mHeader.measure(childWidthMeasureSpec,
						MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
			}
			if(default_type==RelativeType.CENTER) {
				measuredHeight += mHeader.getMeasuredHeight() / 2;
			} else {
				measuredHeight+=mHeader.getMeasuredHeight();
			}
		}

		//measure item
		LayoutParams params = mItem.getLayoutParams();
		//enable hiding listview item,ex. toggle off items in group
		if (mItem.getVisibility() == View.GONE) {
			mItem.measure(childWidthMeasureSpec,
					MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY));
		} else if (params != null && params.height >= 0) {
			mItem.measure(childWidthMeasureSpec,
					MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY));
			measuredHeight += mItem.getMeasuredHeight();
		} else {
			mItem.measure(childWidthMeasureSpec,
					MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
			measuredHeight += mItem.getMeasuredHeight();
		}

		measuredHeight += 2 * item_margin_top_bottom;
		setMeasuredDimension(measuredWidth, measuredHeight);
	}

	/**
	 * 重绘整个item的布局
	 *
	 * @param changed
	 * @param l
	 * @param t
	 * @param r
	 * @param b
	 */
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		l = 0;
		t = 0;
		r = getWidth();
		b = getHeight();

		if (mHeader != null) {
			int headerHeight = mHeader.getMeasuredHeight();
			if(default_type==RelativeType.CENTER) {
				mItem.layout(l, t+item_margin_top_bottom+headerHeight / 2, r, b);
				mHeader.layout(l, t+item_margin_top_bottom, r, headerHeight+item_margin_top_bottom);
			} else {
				mItem.layout(l, t+item_margin_top_bottom+headerHeight, r, b);
				mHeader.layout(l, t+item_margin_top_bottom, r, headerHeight+item_margin_top_bottom);
			}

		} else {
			mItem.layout(l, t, r, b);
		}
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
//		if (mHeader == null && mDivider != null&&mItem.getVisibility()!=View.GONE) {
//			// Drawable.setBounds() does not seem to work pre-honeycomb. So have
//			// to do this instead
//			if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
//				canvas.clipRect(0, 0, getWidth(), mDividerHeight);
//			}
//			mDivider.draw(canvas);
//		}
	}
}
